package com.company;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class TCPClient {

    public static void main(String[] args) throws Exception {
        Socket client = new Socket("192.168.31.120",4603);
        InputStream is = client.getInputStream();
        OutputStream os = client.getOutputStream();
        while(true) {


            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你要发送的信息：");
            String message = sc.nextLine();
            byte[] buf = message.getBytes();
            os.write(buf);
            System.out.println("向服务端发送信息");



            byte[] ans = new byte[1024];
            System.out.println("等待服务端响应……");
            int len = is.read(ans);

            String response = new String(ans,0, len);
            System.out.println("从服务端接收到信息：" + response);
            if(response.equals("Exit"))
                break;
        }
        is.close();
        os.close();
        client.close();
    }
}
